<!DOCTYPE html>

<html>

	<head>
		<meta charset="utf-8">
		<title>角色管理</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../css/common.css" media="all">
	</head>

	<body>

		<div class="layui-fluid">
			<div class="layui-card">
				<div class="layui-card-header table-operate-btn">
					<!-- <span class="marign_right10">全部代理商</span> -->
					<button class="layui-btn layui-btn-normal layui-btn-sm" data-type='alladd'>添加</button>
					<!--<button class="layui-btn layui-btn-danger layui-btn-sm" id="del">删除</button>-->
				</div>
				<div class="layui-card-body">
					
					<table id="test-table-operate" lay-filter="test-table-operate"></table>
					<script type="text/html" id="buttonTpl">
						{{# if(d.check == true){ }}
						<button class="layui-btn layui-btn-xs">已审核</button> {{# } else { }}
						<button class="layui-btn layui-btn-primary layui-btn-xs">未审核</button> {{# } }}
					</script>
					<script type="text/html" id="test-table-operate-barDemo">
						<a class="layui-btn layui-btn layui-btn-xs" lay-event="quanxian">权限</a>
						<!--<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>-->
						<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
					</script>
					
				</div>
			</div>
		</div>

		<script src="../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../js/common.js" type="text/javascript" charset="utf-8"></script>
		<script>
			layui.config({
				base: '../../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'table'], function() {
				var $ = layui.$,
					form = layui.form,
					table = layui.table;

				var _data = {
					type: 'AGENT'
				};

				//用户管理
				table.render({
					elem: '#test-table-operate',
					url: server.querymember_role,
					height: 'full-105',
					cols: [
						[{
								type: 'checkbox',
								fixed: 'left'
							},
							{
								width: 200,
								align: 'center',
								toolbar: '#test-table-operate-barDemo',
								title: '操作',
								fixed: 'left'
							}, {
								field: 'id',
								width: 180,
								title: 'ID',
								sort: true
							},/*{
								field: 'oid',
								title: '序号',
								width: 100
							},*/{
								field: 'name',
								title: '角色名称',
								width: 100
							}, {
								field: 'description',
								title: '描述',
								width: 100
							}, {
								field: 'notes',
								title: '备注',
								width: 100
							}, {
								field: 'edittime',
								title: '创建时间',
								width: 180
							}, {
								field: 'status',
								title: '状态',
								width: 100
							}
						]
					],
					page: true,
					where: _data,
					limit: limit,
					limits: limits,
					request: {
						pageName: 'page',
						limitName: 'size',
					},
					parseData: function(res) { //将原始数据解析成 table 组件所规定的数据
						return {
							"code": res.code, //解析接口状态
							"msg": res.msg, //解析提示文本
							"count": res.member_role_list.pager.total, //解析数据长度
							"data": res.member_role_list.data //解析数据列表
						};
					}
				});

				//搜索角色
				form.on('select(LAY-user-adminrole-type)', function(data) {
					//执行重载
					table.reload('LAY-user-back-role', {
						where: {
							role: data.value
						}
					});
				});

				//监听工具条
				table.on('tool(test-table-operate)', function(obj) {
					var data = obj.data;
					if(obj.event === 'quanxian') {
						layer.open({
							type: 2,
							title: '权限',
							content: '../model/edit_add.html?id=' + data.id+'&role_id='+data.id,
							area: ['70%', '90%'],
							success: function(layero, index) {
								var body = layer.getChildFrame('body', index);
								body.find("input[name=purview1]").val(data.purview);
								body.find("input[name=purview_sub1]").val(data.purview_sub);
								body.find("textarea[name=notes]").val(data.notes);
							}
						});
					}else if(obj.event === 'del') {
						var id = data.id
						layer.confirm('确定要删除该商品？', function(index) {
							submitDelete(id)
						}, function() {
							initdata();
							layer.msg('取消操作');
						});
					}
				});
				function submitDelete(id) {
					$.post(server.deletemember_role, {
						id: id
					}, function(res, status) {
						if(status == 'success') {
							if(res.code == 0) {
								layer.alert('操作成功', function() {
									layer.closeAll(); 
									table.reload('test-table-operate');
								});
							} else {
								layer.msg(res.msg);
								layer.closeAll('loading');
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}

				//事件
				var active = {
					batchdel: function() {
						var checkStatus = table.checkStatus('LAY-user-back-role'),
							checkData = checkStatus.data; //得到选中的数据

						if(checkData.length === 0) {
							return layer.msg('请选择数据');
						}

						layer.confirm('确定删除吗？', function(index) {
							table.reload('LAY-user-back-role');
							layer.msg('已删除');
						});
					},
					alladd: function() {
//						layer.open({
//							type: 2,
//							title: '添加新角色',
//							content: '../model/edit_add.html',
//							area: ['500px', '480px'],
//							btn: ['确定', '取消'],
//							yes: function(index, layero) {
//								var iframeWindow = window['layui-layer-iframe' + index],
//									submit = layero.find('iframe').contents().find("#LAY-user-role-submit");
//
//								//监听提交
//								iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data) {
//									var field = data.field; //获取提交的字段
//
//									//提交 Ajax 成功后，静态更新表格中的数据
//									//$.ajax({});              
//									table.reload('LAY-user-back-role');
//									layer.close(index); //关闭弹层
//								});
//
//								submit.trigger('click');
//							}
//						});
						layer.open({
							type: 2,
							title: '添加角色',
							content: '../model/edit_add.html?role_id=0',
							area: ['70%', '90%'],
							success: function(layero, index) {
							
							}
						});
					}
				}
				$('.table-operate-btn .layui-btn').on('click', function() {
					var type = $(this).data('type');
					active[type] ? active[type].call(this) : '';
				});
			});
		</script>
	</body>

</html>